<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('ACCESS', true);	

session_start();	
	
# set error checking levels #	
	# enable these lines when site is to be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
	#############################
	# include some needed files #
	#############################
		$absRoot = '../../';
		include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
		include($absRoot . 'src/ClientHandle.php');

	$id = $_GET["client_id"];

	$client = new ClientHandle();
	
$MobilesArray=$client->getMobileNumbers($id);

	
# instantiate predifined classes #											
	$xajax = new xajax();

	$xajax->registerFunction("changefields");

	$xajax->registerFunction("editClientMobile");
	
	function changefields($mobilesid, $id) {	
		$statusresponse = new xajaxResponse();

			$client = new ClientHandle();
	
			$MobilesArray=$client->getMobileNumbers($id);

			$mobilesid--;
			
			$Value=$MobilesArray[$mobilesid];
			
		$statusresponse->assign('Priority', 'value', $Value['mobile_priority']);

		$statusresponse->assign('CountryCode', 'value', $Value['mobile_countrycode']);
		
		$statusresponse->assign('Provider', 'value', $Value['mobile_provider']);
		
		$statusresponse->assign('MobileNumber', 'value', $Value['mobile_number']);
		
		return $statusresponse;
	}
	
		function editClientMobile($mobileid, $priority, $countrycode, $provider, $mobilenumber, $id) {
		$statusresponse = new xajaxResponse();
	
		# check if the fields are empty
			if (empty($priority) || empty($countrycode) || empty($provider) || empty($mobilenumber)) {
				$statusresponse->assign('Status','style.color','red');
				$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br /> Not updating..');
			}
			else {
			
			$client = new ClientHandle();
	
			$MobilesArray=$client->getMobileNumbers($id);

			$mobileid--;
			
			$Mobile=$MobilesArray[$mobileid];
							
			$PhnId=$Mobile['mobile_id'];
			
			$Priority=$Mobile['mobile_priority'];
			
			$CountryCode=$Mobile['mobile_countrycode'];
			
			$Provider=$Mobile['mobile_provider'];
			
			$MobileNumber=$Mobile['mobile_number'];
			
			
					if ($Priority == $priority && $CountryCode == $countrycode && $Provider == $provider && $MobileNumber == $mobilenumber) {
						$statusresponse->assign('Status','style.color','orange');
						$statusresponse->assign('Status', 'innerHTML', 'No changes are made.<br /> Not updating..');
					}
					elseif ($id != '') {
							
						$result = $client->setMobileNumberValue($PhnId, 'mobile_priority', $priority, $id);
						$result = $client->setMobileNumberValue($PhnId, 'mobile_countrycode', $countrycode, $id);
						$result = $client->setMobileNumberValue($PhnId, 'mobile_provider', $provider, $id);
						$result = $client->setMobileNumberValue($PhnId, 'mobile_number', $mobilenumber, $id);
						
						
						if($result)
						{ 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Address changed.<br /> Database updated.');
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()");
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Please complete all fields.<br />Not updating..');
						}
					}
			
			}
			
								
		return $statusresponse;
	}
	
	$xajax->processRequest();	


	//print_r($result);
?>

<html>
<head>
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>	
</head>
<body>											

<form id="EditClientMobiles" action="#" method='post'>
<table>

	<tr>
	<td>
	Mobile:</td>
	<td>
			<select id="Number" name="Number" onChange="xajax_changefields(document.getElementById('Number').value, <?php echo $id;?>);">
//Gathering the Address IDs
<?php
$phnsctr=0;
$initial=-1;	
while($phnsctr < sizeof($MobilesArray))
{
$Temp=$MobilesArray[$phnsctr];
if($initial==-1)
{
$initial=$phnsctr;
$ToEdit=$Temp;
}
?>
<option value="<?php echo $phnsctr+1; ?>"><?php echo $phnsctr+1; ?></option>
<?php $phnsctr++; } ?>			
		</select>
	</td>
	</tr>
	
	<tr>
	<td>
	Priority:</td>
	<td>
		<input type=text id="Priority" name="Priority" value="<?php echo $ToEdit['mobile_priority'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Country Code:</td>
	<td>
		<input type=text id="CountryCode" value="<?php echo $ToEdit['mobile_countrycode'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Provider:</td>
	<td>
	<input type=text id="Provider" value="<?php echo $ToEdit['mobile_provider'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Number:</td>
	<td>
	<input type=text id="MobileNumber" value="<?php echo $ToEdit['mobile_number'];?>"></input>
	</td>
	</tr>

	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type=button id="Update" value="Update" onClick="xajax_editClientMobile(document.getElementById('Number').value, document.getElementById('Priority').value, document.getElementById('CountryCode').value, document.getElementById('Provider').value, document.getElementById('MobileNumber').value, <?php echo $id;?>);"></input>
	<input type=reset id="Reset" value="Reset"></input>
	</td>	
	</tr>
	
</table>
</form>

<div id='Status'></div>

</body>

</html>